<!--
 * @Author: your name
 * @Date: 2021-03-15 20:53:56
 * @LastEditTime: 2021-03-15 20:56:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edi
 * @FilePath: \study\js\面试题\数组计算.html
-->
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>

    <body>
        <h1>setInterval是否可以完整正确的按照设置的时间隔执行?如果不 能,请编写正确的 dosetInterval函数,要求和 setInterval工程相</h1>
        <!-- <p>https://www.cnblogs.com/benjytrys/archive/2013/04/25/3043175.html</p> -->
        <script>
            // var mySetInterval = function (cb, delay) {
            //     console.log('mySetInterval()');
            //     cb();
            //     setTimeout(() => {
            //         console.log('setTimeout()');
            //         mySetInterval(cb, delay);
            //     }, delay);
            // };

            let interval = setInterval(() => {
                console.log('interval');
            }, 5000);
            console.log(interval);
            interval2 = setInterval(() => {
                console.log('interval2');
            }, 5000);
            console.log(interval);
            interval3 = setInterval(() => {
                console.log('interval3');
            }, 5000);
            console.log(interval3);

            requestAnimationFrame(() => {
                console.log('requestAnimationFrame');
            }, 60);

            var mySetInterval = function (cb, delay) {
                var recurse;
                var ref = {}; // 作为 返回值
                ref['continue'] = true;
                (recurse = function () {
                    if (ref['continue']) {
                        ref.timeout = setTimeout(function () {
                            cb();
                            recurse();
                        }, delay);
                    }
                })();
                return ref;
            };
            var clearTimer = function (ref) {
                ref['continue'] = false;
                clearTimeout(ref.timeout);
            };

            mySetInterval(function () {
                console.log(Date.now());
            }, 3000);
        </script>
    </body>
</html>
